//
//  MyInformationViewController.m
//  家校互通
//
//  Created by 李赛 on 15/6/1.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "MyInformationViewController.h"
#import "MyInfoTitleEditViewCell.h"
#import "MyInfoHeaderViewCell.h"
#import "InfomationEditViewController.h"
#import "APService.h"
@interface MyInformationViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    
}
@property (nonatomic, retain)NSMutableArray  *titleArray;
@property (nonatomic, retain)NSMutableArray  *infoDetailArray;;
@property (nonatomic, retain) UIImage *photoImage;
@end

@implementation MyInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"个人资料";
    _contentTableView.delegate=self;
    _contentTableView.dataSource=self;

//    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 44)];
//    [button setTitle:@"修改个人资料" forState:UIControlStateNormal];
//    [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
//    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    
//    [button addTarget:self action:@selector(editUserInfoAction:) forControlEvents:UIControlEventTouchUpInside];
//    
//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    if ([DSFUserInfo shareUserInstance].isTeachLogin) {
        _titleArray = [NSMutableArray arrayWithObjects:@"姓名",@"身份",@"班级", nil];
        _infoDetailArray=[NSMutableArray arrayWithObjects:@"赵老师",@"班主任",@"三年二班", nil];
    }else
    {
        _titleArray = [NSMutableArray arrayWithObjects:@"孩子姓名",@"与孩子关系",@"班级",@"手机",@"住址", nil];
        _infoDetailArray=[NSMutableArray arrayWithObjects:@"王小明",@"爸爸",@"三年二班",@"1359999999" ,@"北京市朝阳区123456小区2号楼212室",nil];
    }
    // Do any additional setup after loading the view.
}

-(void)editUserInfoAction:(UIButton *)sender
{
    InfomationEditViewController *vc = [self.storyboard  instantiateViewControllerWithIdentifier:@"InfomationEditViewController"];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ( [DSFUserInfo shareUserInstance].isTeachLogin) {
        if (section==0) {
            return 1;
        }else
        {
            return 3;
        }
     
    }else
    {
        if (section==0) {
            return 1;
        }else if(section==1)
        {
            return 3;
        }else
        {
            return 2;
        }
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        MyInfoHeaderViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"MyInfoHeaderViewCell" forIndexPath:indexPath];
        [vCell.userHeaderImg sd_setBackgroundImageWithURL:[NSURL URLWithString:[DSFUserInfo shareUserInstance].icon] forState:UIControlStateNormal  placeholderImage:[UIImage imageNamed:@"icon01.jpg"]];
     
        UIView *selectView=[[UIView alloc]initWithFrame:vCell.frame];
        selectView.backgroundColor=COLOR(240, 240, 240);
        vCell.selectedBackgroundView=selectView;
        return vCell;
    }
    else
    {
        MyInfoTitleEditViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"MyInfoTitleEditViewCell" forIndexPath:indexPath];
        UIView *selectView=[[UIView alloc]initWithFrame:vCell.frame];
        selectView.backgroundColor=COLOR(240, 240, 240);
        vCell.selectedBackgroundView=selectView;
        if ( [DSFUserInfo shareUserInstance].isTeachLogin) {
      
            vCell.infoDescriptionLB.text=[_titleArray objectAtIndex: indexPath.row];
            vCell.infoLB.text=[_infoDetailArray objectAtIndex:indexPath.row];
        }else
        {
            if(indexPath.section==1)
            {
                vCell.infoDescriptionLB.text=[_titleArray objectAtIndex:indexPath.row];
                vCell.infoLB.text=[_infoDetailArray objectAtIndex:indexPath.row];
            }
            else
            {
                vCell.infoDescriptionLB.text=[_titleArray objectAtIndex:3+ indexPath.row];
                vCell.infoLB.text=[_infoDetailArray objectAtIndex:3+ indexPath.row];
            }
        }
        return vCell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if([DSFUserInfo shareUserInstance].isTeachLogin)
        return 2;
    else
        return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        //个人详情页面
        UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择",nil];
        [sheet showInView:self.view];
    }
    else
    {
       
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if(2==buttonIndex)
        return;

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate =self;
    picker.allowsEditing=YES;
    
    if(1  == buttonIndex) {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else if(0 == buttonIndex) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    
    [picker dismissModalViewControllerAnimated:YES];
   NSString *registrationId = [APService registrationID];
    
    [_contentTableView reloadData];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSMutableArray *imageArr = [[NSMutableArray alloc] initWithObjects:image, nil];
    
    [CoModel uploadImage:^(BOOL result, NSMutableArray *imageUrlList, NSError *error) {
        if (result) {
            [DSFUserInfo shareUserInstance].icon=imageUrlList[0];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [CoModel modify:^(BOOL result, NSString *failMessage, NSError *error) {
                if (result)
                {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    [_contentTableView reloadData];
                    [[NSNotificationCenter defaultCenter] postNotificationName:kReloadMyInfo object:nil];
                }else
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
            } withIcon:imageUrlList[0] andPushId:registrationId];
        }else
            [MBProgressHUD hideHUDForView:self.view animated:YES];
    } withImage:imageArr andType:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
